package com.project.service;

import com.project.dao.StudentInfoMapper;
import com.project.entity.*;
import com.project.entity.Class;
import com.project.myBeans.StudentInfoDetail;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by WheelChen on 2017/3/20.
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class StudentInfoServiceImpl extends BaseService implements StudentInfoService {
    @Autowired
    UniversityService universityService;
    @Autowired
    SchoolService schoolService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    MajorService majorService;
    @Autowired
    ClassService classService;
    @Autowired
    TeacherInfoService teacherInfoService;
    @Autowired
    private UserService userService;

    private static StudentInfoMapper studentInfoMapper = null;

    public void setMapper() {
        if (studentInfoMapper == null)
            studentInfoMapper = sqlSessionTemplate.getMapper(StudentInfoMapper.class);//1.获取相关mapper
    }


    @Override
    public int modifyByUserIdSelective(StudentInfo studentInfo) {
        setMapper();
        return studentInfoMapper.updateByUserIdSelective(studentInfo);
    }

    @Override
    public StudentInfo findOneByUserId(Integer userId) {
        setMapper();
        return studentInfoMapper.selectByUserId(userId);
    }

    /**
     * author: WheelChen
     * date: 2017/7/28
     * function: 根据id修改学生信息表
     *
     * @param studentInfo
     * @return
     */
    @Override
    public int modifyOneById(StudentInfo studentInfo) {
        setMapper();
        return studentInfoMapper.updateByPrimaryKeySelective(studentInfo);
    }

    /**
     * author: WheelChen
     * date: 2017/6/30
     * function: 根据id查询学生信息
     *
     * @param id
     * @return
     */
    @Override
    public StudentInfo findById(Integer id) {
        setMapper();
        return studentInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017/7/1
     * function: 根据userId查找学生详情信息
     *
     * @param userId
     * @return
     */
    @Override
    public StudentInfoDetail findDetailByUserId(Integer userId) throws Exception {
        setMapper();
        StudentInfoDetail studentInfoDetail = new StudentInfoDetail();
        Class clazz = null;
        User user;
        studentInfoDetail.setUser(userService.findOneByUserID(userId));
        StudentInfo studentInfo = findOneByUserId(userId);
        studentInfoDetail.setStudentInfo(studentInfo);
        if (studentInfo != null) {
            studentInfoDetail.setStudentInfo(studentInfo);
            if (studentInfo.getClassid() != null) {
                clazz = classService.findOne(studentInfo.getClassid());
                studentInfoDetail.setClazz(clazz);
            }
            //实习指导老师
            if (studentInfo.getInternadvisor() != null) {
                //先找到教师信息表
                TeacherInfo teacherInfo = teacherInfoService.findOne(studentInfo.getInternadvisor());
                //再找到用户表
                if (teacherInfo != null) {
                    user = userService.findOneByUserID(teacherInfo.getUserid());
                    studentInfoDetail.setTeacherInfoInternName(user.getRealname());
                }
            }

            //毕设指导老师
            if (studentInfo.getPaperadvisor() != null) {
                //先找到教师信息表
                TeacherInfo teacherInfo = teacherInfoService.findOne(studentInfo.getPaperadvisor());
                //再找到用户表
                if (teacherInfo != null) {
                    user = userService.findOneByUserID(teacherInfo.getUserid());
                    studentInfoDetail.setTeacherInfoPaperName(user.getRealname());
                }
            }
            if (clazz != null) {
                if (clazz.getMajorid() != null)
                    studentInfoDetail.setMajor(majorService.findOne(clazz.getMajorid()));
                if (clazz.getDepartmentid() != null)
                    studentInfoDetail.setDepartment(departmentService.findOne(clazz.getDepartmentid()));
                if (clazz.getSchoolid() != null)
                    studentInfoDetail.setSchool(schoolService.findOne(clazz.getSchoolid()));
                if (clazz.getUniversityid() != null)
                    studentInfoDetail.setUniversity(universityService.findOne(clazz.getUniversityid()));
            }
        }
        return studentInfoDetail;
    }

    /**
     * author: WheelChen
     * date: 2017/6/30
     * function: 根据学生信息表id查找学生详情信息
     *
     * @param id 学生信息表id
     * @return
     */
    @Override
    public StudentInfoDetail findDetailById(Integer id) throws Exception {
        User user;
        StudentInfoDetail studentInfoDetail = new StudentInfoDetail();
        StudentInfo studentInfo = findById(id);
        studentInfoDetail.setStudentInfo(studentInfo);
        studentInfoDetail.setUser(userService.findOneByUserID(studentInfo.getUserid()));
        Class clazz = null;
        if (studentInfo != null) {
            studentInfoDetail.setStudentInfo(studentInfo);
            if (studentInfo.getClassid() != null) {
                clazz = classService.findOne(studentInfo.getClassid());
                studentInfoDetail.setClazz(clazz);
            }
            //实习指导老师
            if (studentInfo.getInternadvisor() != null) {
                //先找到教师信息表
                TeacherInfo teacherInfo = teacherInfoService.findOne(studentInfo.getInternadvisor());
                //再找到用户表
                if (teacherInfo != null) {
                    user = userService.findOneByUserID(teacherInfo.getUserid());
                    studentInfoDetail.setTeacherInfoInternName(user.getRealname());
                }
            }
            //毕设指导老师
            if (studentInfo.getAdvisestate() != null) {
                //先找到教师信息
                TeacherInfo teacherInfo = teacherInfoService.findOne(studentInfo.getPaperadvisor());
                //再找到用户表
                if (teacherInfo != null) {
                    user = userService.findOneByUserID(teacherInfo.getUserid());
                    studentInfoDetail.setTeacherInfoPaperName(user.getRealname());
                }
            }
            if (clazz != null) {
                if (clazz.getMajorid() != null)
                    studentInfoDetail.setMajor(majorService.findOne(clazz.getMajorid()));
                if (clazz.getDepartmentid() != null)
                    studentInfoDetail.setDepartment(departmentService.findOne(clazz.getDepartmentid()));
                if (clazz.getSchoolid() != null)
                    studentInfoDetail.setSchool(schoolService.findOne(clazz.getSchoolid()));
                if (clazz.getUniversityid() != null)
                    studentInfoDetail.setUniversity(universityService.findOne(clazz.getUniversityid()));
            }
        }
        return studentInfoDetail;
    }

    /**
     * author: WheelChen
     * date: 2017/7/6
     * function: 模糊查找 + 分页
     *
     * @param studentInfo
     * @return
     */
    @Override
    public List<StudentInfo> findBatchByCondition(StudentInfo studentInfo) {
        setMapper();
        return studentInfoMapper.selectByCondition(studentInfo);
    }

    /**
     * author: WheelChen
     * date: 2017/7/26
     * function: 根据学校实习指导老师的教师信息表id计算被指导的实习学生人数
     *
     * @param internAdvisor 学校实习指导老师的教师信息表id
     * @return
     */
    @Override
    public int countByInternAdvisor(Integer internAdvisor) {
        setMapper();
        return studentInfoMapper.countByInternAdvisor(internAdvisor);
    }

    /**
     * author: WheelChen
     * date: 2017/7/28
     * function: 新增学生详细信息 （影响用户表 + 学生信息表）
     *
     * @param studentInfoDetail 学生详细信息
     * @return
     */
    @Override
    public void addDetailOne(StudentInfoDetail studentInfoDetail) throws Exception {
        //update用户表
        if (studentInfoDetail.getUser() != null)
            userService.updateUser(studentInfoDetail.getUser());
        //insert学生信息表
        if (studentInfoDetail.getStudentInfo() != null)
            add(studentInfoDetail.getStudentInfo());
    }

    /**
     * author: WheelChen
     * date: 2017/7/29
     * function:  修改学生详细信息 （影响用户表 + 学生信息表）
     *
     * @param studentInfoDetail
     * @return
     */
    @Override
    public void modifyDetailOne(StudentInfoDetail studentInfoDetail) throws Exception {
        //update用户表
        if (studentInfoDetail.getUser() != null)
            userService.updateUser(studentInfoDetail.getUser());
        //update学生信息表
        if (studentInfoDetail.getStudentInfo() != null)
            modifyOneById(studentInfoDetail.getStudentInfo());
    }

    /**
     * author: WheelChen
     * date: 2017/7/1
     * function: 根据学生信息表记录查找学生详情信息 （）
     *
     * @param studentInfo
     * @return
     */
    @Override
    public StudentInfoDetail findDetailByStudentInfo(StudentInfo studentInfo) throws Exception {
        StudentInfoDetail studentInfoDetail = new StudentInfoDetail();
        studentInfoDetail.setStudentInfo(studentInfo);
        if (studentInfo.getUserid() != null)
            studentInfoDetail.setUser(userService.findOneByUserID(studentInfo.getUserid()));
        else
            throw new Exception("学生信息表缺少userId");
        studentInfoDetail.setStudentInfo(studentInfo);
        if (studentInfo.getDepartmentid() != null)
            studentInfoDetail.setDepartment(departmentService.findOne(studentInfo.getDepartmentid()));
        if (studentInfo.getMajorid() != null)
            studentInfoDetail.setMajor(majorService.findOne(studentInfo.getMajorid()));
        if (studentInfo.getClassid() != null)
            studentInfoDetail.setClazz(classService.findOne(studentInfo.getClassid()));
//        //实习指导老师
//        if (studentInfo.getInternadvisor() != null) {
//            //先找到教师信息表
//            TeacherInfo teacherInfo = teacherInfoService.findOne(studentInfo.getInternadvisor());
//            //再找到用户表
//            if (teacherInfo != null) {
//                user = userService.findOneByUserID(teacherInfo.getUserid());
//                studentInfoDetail.setTeacherInfoInternName(user.getRealname());
//            }
//        }


//        if (studentInfo.getSchoolid() != null)
//            studentInfoDetail.setSchool(schoolService.findOne(studentInfo.getSchoolid()));

        return studentInfoDetail;
    }

    /**
     * author: WheelChen
     * date: 2017/9/18
     * function: 根据系id批量查找学生信息
     *
     * @param departmentId
     * @return
     */
    @Override
    public List<StudentInfo> findBatchByDepartmentId(Integer departmentId) {
        return studentInfoMapper.selectBatchByDepartmentId(departmentId);
    }

    /**
     * author: WheelChen
     * date: 2017/9/18
     * function: 根据专业id批量查找学生信息
     *
     * @param majorId
     * @return
     */
    @Override
    public List<StudentInfo> findBatchByMajorId(Integer majorId) {
        return studentInfoMapper.selectBatchByMajorId(majorId);
    }

    /**
     * author: WheelChen
     * date: 2017/9/16
     * function: 根据班级id查找学生信息
     *
     * @param classId
     * @return
     */
    @Override
    public List<StudentInfo> findBatchByClassId(Integer classId) {
        return studentInfoMapper.selectBatchByClassId(classId);
    }

    /**
     * author: WheelChen
     * date: 2017/9/18
     * function: 根据系id批量删除学生表信息
     *
     * @param departmentId
     * @return
     */
    @Override
    public int removeBatchByDepartmentId(Integer departmentId) {
        return studentInfoMapper.deleteBatchByDepartmentId(departmentId);
    }

    /**
     * author: WheelChen
     * date: 2017/9/18
     * function: 根据专业id批量删除学生表信息
     *
     * @param majorId
     * @return
     */
    @Override
    public int removeBatchByMajorId(Integer majorId) {
        return studentInfoMapper.deleteBatchByMajorId(majorId);
    }

    /**
     * author: WheelChen
     * date: 2017/9/16
     * function: 根据班级id批量删除学生表信息
     *
     * @param classId
     * @return
     */
    @Override
    public int removeBatchByClassId(Integer classId) {
        return studentInfoMapper.deleteBatchByClassId(classId);
    }

//    @Override
//    public List<StudentInfoDetail> findDetailByCondition(StudentInfoDetail studentInfoDetail) {
//        setMapper();
//        return studentInfoMapper.selectDetailByCondition(studentInfoDetail);
//    }


//    @Override
//    public int save(String jsonStr) {
//        StudentInfo studentInfo = null;
//        University university = null;
//        School school = null;
//        Department department = null;
//        Major major = null;
//        com.project.entity.Class aClass= null;
//        TeacherInof teacherInofIntern = null;
//        TeacherInof teacherInofPaper = null;
//
//        JSONObject jsonObject = JSON.parseObject(jsonStr);
//        if (jsonObject.getString("studentInfo") != null)
//            studentInfo = JSONObject.parseObject(jsonObject.getString("studentInfo"), StudentInfo.class);
//        if (jsonObject.getString("university") != null)
//            university = JSONObject.parseObject(jsonObject.getString("university"), University.class);
//        if (jsonObject.getString("school") != null)
//            school = JSONObject.parseObject(jsonObject.getString("school"), School.class);
//        if (jsonObject.getString("department") != null)
//            department = JSONObject.parseObject(jsonObject.getString("department"), Department.class);
//        if (jsonObject.getString("major") != null)
//            major = JSONObject.parseObject(jsonObject.getString("major"), Major.class);
//        if (jsonObject.getString("aClass") != null)
//            aClass = JSONObject.parseObject(jsonObject.getString("aClass"), com.project.entity.Class.class);
//        if (jsonObject.getString("teacherInofIntern") != null)
//            teacherInofIntern = JSONObject.parseObject(jsonObject.getString("teacherInofIntern"), TeacherInof.class);
//        if (jsonObject.getString("teacherInofPaper") != null)
//            teacherInofPaper = JSONObject.parseObject(jsonObject.getString("teacherInofPaper"), TeacherInof.class);
//
//
//        modifyByUserIdSelective(studentInfo);
//
//        return 0;
//    }

    @Override
    public int add(StudentInfo studentInfo) {
        setMapper();
        return studentInfoMapper.insertSelective(studentInfo);
    }

    @Override
    public int removeOneByUserId(Integer userId) {
        setMapper();
        return studentInfoMapper.deleteByUserId(userId);
    }

}
